package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.musicroom.UserMusicRoomAppraiseData;
import com.yanqu.road.entity.musicroom.UserMusicRoomBeautyData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMusicRoomAppraiseDaoImpl extends TempDataObjectDao<UserMusicRoomAppraiseData> {

    public static String getUserMusicRoomAppraiseDataSql = "select * from `t_u_music_room_appraise` where `userId`=?";
    public List<UserMusicRoomAppraiseData> getUserMusicRoomAppraiseData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserMusicRoomAppraiseData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMusicRoomAppraiseDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMusicRoomAppraiseData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }


    public static String updateUserMusicRoomAppraiseDataSql = "update `t_u_music_room_appraise` set buildId=?,furnitureId=?,beautyId=?,appraiseTime=?,status=?,newStatus=?,targetLevel=? where userId=? and appraiseId=?";
    @Override
    protected boolean updateData(UserMusicRoomAppraiseData userMusicRoomAppraiseData) {
        userMusicRoomAppraiseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getBuildId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getFurnitureId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getBeautyId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomAppraiseData.getAppraiseTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getNewStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getTargetLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomAppraiseData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getAppraiseId()));
        if (getDb().executeNoneQuery(updateUserMusicRoomAppraiseDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMusicRoomAppraiseDataSql = "insert into `t_u_music_room_appraise`(userId,appraiseId,buildId,furnitureId,beautyId,appraiseTime,status,newStatus,targetLevel) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserMusicRoomAppraiseData userMusicRoomAppraiseData) {
        userMusicRoomAppraiseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomAppraiseData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getAppraiseId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getBuildId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getFurnitureId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getBeautyId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomAppraiseData.getAppraiseTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getNewStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomAppraiseData.getTargetLevel()));
        if (getDb().executeNoneQuery(insertUserMusicRoomAppraiseDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMusicRoomAppraiseData parseData(ResultSet rs) throws SQLException {
        UserMusicRoomAppraiseData userMusicRoomAppraiseData = new UserMusicRoomAppraiseData();
        userMusicRoomAppraiseData.setUserId(rs.getLong("userId"));
        userMusicRoomAppraiseData.setAppraiseId(rs.getInt("appraiseId"));
        userMusicRoomAppraiseData.setBuildId(rs.getInt("buildId"));
        userMusicRoomAppraiseData.setFurnitureId(rs.getInt("furnitureId"));
        userMusicRoomAppraiseData.setBeautyId(rs.getInt("beautyId"));
        userMusicRoomAppraiseData.setAppraiseTime(rs.getLong("appraiseTime"));
        userMusicRoomAppraiseData.setStatus(rs.getInt("status"));
        userMusicRoomAppraiseData.setNewStatus(rs.getInt("newStatus"));
        userMusicRoomAppraiseData.setTargetLevel(rs.getInt("targetLevel"));

        return userMusicRoomAppraiseData;
    }
}
